const Mod = require("./model");
const { power } = require("../../config/baseConfig");
const {
  addData,
  updateData,
  findData,
  delData,
} = require("../../command/command");
const {
  message,
  requestLevel: { success, warn, fail, error },
} = require("../../utils").message;
const { createToken } = require("../../utils/token");
const userDbKey = require("../../db/user").data;
exports.adminLogin = (req, res, next) => {
  const { params } = res;
  findData(Mod, params)
    .then((data) => {
      data[0] &&
        (data[0].userType === "super" || data[0].userType === "admin") &&
        message({
          res,
          type: success,
          msg: "登录成功",
          data: { token: createToken(data[0].userType, data[0]._id) },
        });
      !data[0] &&
        message({ res, type: fail, msg: "登录失败，用户名或密码错误" });
      next();
    })
    .catch((err) => {
      message({ res, type: error, msg: err });
      next();
    });
};
exports.findUser = (req, res, next) => {
  const { params } = res;
  const { decoded } = params;
  if (decoded.type !== "super" && decoded.type !== "admin") {
    message({ res, type: fail, msg: "无访问权限" });
    next();
    return;
  }
  findData(Mod, params)
    .then((data) => {
      message({
        res,
        type: success,
        data: {
          list: data,
          userDbKey,
          power: power["userManage"][decoded.type],
        },
      });
      next();
    })
    .catch((err) => {
      message({ res, type: error, msg: err });
      next();
    });
};
exports.addUser = (req, res, next) => {
  const { params } = res;
  const { decoded } = params;
  if (decoded.type !== "super") {
    message({ res, type: fail, msg: "无访问权限" });
    next();
    return;
  }
  addData(Mod, params)
    .then((data) => {
      message({
        res,
        type: success,
        data: {},
      });
      next();
    })
    .catch((err) => {
      message({ res, type: error, msg: err });
      next();
    });
};
exports.delUser = (req, res, next) => {
  const { params } = res;
  const { decoded, _id } = params;
  if (decoded.type !== "super") {
    message({ res, type: fail, msg: "无访问权限" });
    next();
    return;
  }
  delData(Mod, _id)
    .then((data) => {
      console.log(data);
      data &&
        data.deletedCount > 0 &&
        message({
          res,
          type: success,
          msg: "删除成功",
        });
      (!data || data.deletedCount <= 0) &&
        message({ res, type: fail, msg: "删除失败" });
      next();
    })
    .catch((err) => {
      message({ res, type: error, msg: err });
      next();
    });
};
exports.updateUser = (req, res, next) => {
  const { params } = res;
  const { decoded, _id } = params;
  if (decoded.type !== "super") {
    message({ res, type: fail, msg: "无访问权限" });
    next();
    return;
  }
  updateData(Mod, _id, params)
    .then((data) => {
      data &&
        message({
          res,
          type: success,
          msg: "修改成功",
        });
      !data && message({ res, type: fail, msg: "修改失败" });
      next();
    })
    .catch((err) => {
      message({ res, type: error, msg: err });
      next();
    });
};
